package com.wanban.downloader.db.room

import android.text.TextUtils
import androidx.room.TypeConverter
import com.google.gson.Gson
import com.wanban.downloader.model.M3U8
import com.wanban.downloader.model.M3U8Seg
import com.wanban.downloader.model.M3U8Stream
import org.json.JSONObject

class M3U8Converters {

    @TypeConverter
    fun stringToList(value: String?): M3U8? {
        return if (value == null || TextUtils.isEmpty(value)) {
            null
        } else {
            Gson().fromJson(value, M3U8::class.java)
        }

    }

    @TypeConverter
    fun listToString(data: M3U8?): String? {
        return if (data != null) {
            Gson().toJson(data)
        } else {
            null
        }
    }
    private fun toM3U8(value: String): M3U8{
        val m3U8 = M3U8()
        val jsonObject = JSONObject(value)
        m3U8.videoName = jsonObject.optString("a")
        m3U8.finalUrl = jsonObject.optString("b")
        m3U8.errorMsg = jsonObject.optString("c")
        m3U8.success = jsonObject.optBoolean("d")
        m3U8.version = jsonObject.optInt("e")
        m3U8.sequence = jsonObject.optInt("f")
        m3U8.errorCode = jsonObject.optInt("g")
        m3U8.targetDuration = jsonObject.optDouble("h").toFloat()
        m3U8.isM3U8 = jsonObject.optBoolean("i")
        if (jsonObject.has("j")) {
            val meu8SegJsonArray = jsonObject.getJSONArray("j")
            val length = meu8SegJsonArray.length()
            m3U8.tsList = ArrayList(length)
            for (i in 0 until length) {
                val m3u8Seg = M3U8Seg()
                val m3u8SegJsonObject = meu8SegJsonArray.getJSONObject(i) ?: continue
                m3u8Seg.url = m3u8SegJsonObject.optString("a")
                m3u8Seg.name = m3u8SegJsonObject.optString("b")
                m3u8Seg.method = m3u8SegJsonObject.optString("c")
                m3u8Seg.keyUri = m3u8SegJsonObject.optString("d")
                m3u8Seg.keyIV = m3u8SegJsonObject.optString("e")
                m3u8Seg.initSegmentUri = m3u8SegJsonObject.optString("f")
                m3u8Seg.segmentByteRange = m3u8SegJsonObject.optString("g")
                m3u8Seg.index = m3u8SegJsonObject.optInt("h")
                m3u8Seg.retryCount = m3u8SegJsonObject.optInt("i")
                m3u8Seg.tsSize = m3u8SegJsonObject.optLong("j")
                m3u8Seg.contentLength = m3u8SegJsonObject.optLong("k")
                m3u8Seg.duration = m3u8SegJsonObject.optDouble("l").toFloat()
                m3u8Seg.hasDiscontinuity = m3u8SegJsonObject.optBoolean("m")
                m3u8Seg.hasKey = m3u8SegJsonObject.optBoolean("n")
                m3u8Seg.hasInitSegment = m3u8SegJsonObject.optBoolean("o")
                m3u8Seg.complete = m3u8SegJsonObject.optBoolean("p")
                m3u8Seg.mp4StartIndex = m3u8SegJsonObject.optLong("q")
                m3u8Seg.mp4EndIndex = m3u8SegJsonObject.optLong("r")
                m3u8Seg.isError = m3u8SegJsonObject.optBoolean("s")

                m3U8.tsList?.add(m3u8Seg)
            }
        }

        if (jsonObject.has("k")) {
            val streamsJsonArray = jsonObject.getJSONArray("k")
            val length = streamsJsonArray.length()
            m3U8.streams = ArrayList(length)
            for (i in 0 until length) {
                val stream = M3U8Stream()
                val m3u8SegJsonObject = streamsJsonArray.getJSONObject(i) ?: continue
                stream.peakBitrate = m3u8SegJsonObject.optInt("a")
                stream.averageBitrate = m3u8SegJsonObject.optInt("b")
                stream.width = m3u8SegJsonObject.optInt("c")
                stream.height = m3u8SegJsonObject.optInt("d")
                stream.frameRate = m3u8SegJsonObject.optDouble("e").toFloat()
                stream.url = m3u8SegJsonObject.optString("f")

                m3U8.streams?.add(stream)
            }
        }
        return m3U8
    }
}